package com.andnnl.date;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.ReUtil;
import com.xkzhangsan.time.calculator.DateTimeCalculatorUtil;
import com.xkzhangsan.time.formatter.DateFormatPattern;
import com.xkzhangsan.time.formatter.DateTimeFormatterUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * Created by chenss on 2021/1/5.
 */
public class DateParseTest {
    static Pattern REGEX_AND = Pattern.compile("sendtime *?([><=]{0,2}) *?' *?(\\d{4}-\\d{1,2}.*?)' *?and.*?([><=]{0,2}) *?' *?(\\d{4}-\\d{1,2}.*?)'", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
    static Pattern REGEX_BETWEEN = Pattern.compile("sendtime *?between *?' *?(\\d{4}-\\d{1,2}.*?)' *?and.*?' *?(\\d{4}-\\d{1,2}.*?)'", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
    public static void main(String[] args) {

        Date lastMonth=DateTimeCalculatorUtil.plusMonths(DateTimeFormatterUtil.smartParseToDate("2021-3-31 12:2:32.123"),-1);//1个月
        System.out.println(DateTimeFormatterUtil.format(lastMonth,DateFormatPattern.YYYY_MM_DD_HH_MM_SS_SSS));

        String[] ds = new String[]{
                "2020-1-2 12:2:32.123",
                "2020-1-2 2:2:2.123",
                "2020-10-20 12:2:32",
                "2020-1-2",
                "2020-1-31"
        };
        for (String str : ds) {

//            Date d = DateTimeFormatterUtil.parseToDate(str, new String[]{DateFormatPattern.YYYY_MM_DD_HH_MM_SS_SSS
//                    , DateFormatPattern.YYYY_MM_DD_HH_MM_SS, DateFormatPattern.YYYY_MM_DD});
//            System.out.println(d);

            Date d2 = DateTimeFormatterUtil.smartParseToDate(str);
            System.out.println(DateTimeFormatterUtil.format(d2,DateFormatPattern.YYYY_MM_DD_HH_MM_SS_SSS));


        }

        Date beforeDay7=DateTimeCalculatorUtil.plusDays(new Date(),-7);//7天之前
//        beforeDay7=DateTimeCalculatorUtil.reduceAccuracyToDay(beforeDay7);//
        System.out.println(DateTimeFormatterUtil.format(beforeDay7,DateFormatPattern.YYYY_MM_DD_HH_MM_SS_SSS));
        beforeDay7=DateTimeCalculatorUtil.reduceAccuracyToDay(beforeDay7);
        System.out.println("------------------------");
        String content1 = "SendTime >= '2020-2-15 00:00:00'  and  SendTime <= '2020-4-15 23:59:59' and eprId>0 order by sendTime desc";
        String content2 = "SendTime <= '2020-6-15 00:00:00'  and  SendTime >= '2020-3-13 23:59:59' and eprId>0 order by sendTime desc";
        String content3 = "SendTime <= '2021-1-5 00:00:00'  and  SendTime >= '2020-12-13 23:59:59' and eprId>0 order by sendTime desc";
        String content4 = "SendTime between '2020-3-15 00:00:00'  and  '2020-4-15 23:59:59' and eprId>0 order by sendTime desc";
        String content5 = "SendTime between '2020-12-29 00:00:00'  and  '2021-1-1 23:59:59' and eprId>0 order by sendTime desc";

        Stream.of(content1,content2,content3,content4,content5)
                .forEach(content->{
            List<Date> list = getDates(content);
            list.forEach(d-> System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(d)));
            Tuple tuple = getBeforeDay7(list);
            System.out.println(tuple);
//            Boolean t = tuple.get(0);
//            Date dd = tuple.get(1);
//            String msg = tuple.get(2);
//            System.out.println(t);
//            System.out.println(dd);
//            System.out.println(msg);
            System.out.println("------------------------");
        });

    }

    /**
     * 判断时间范围是否在包括第7天前
     * @param list
     * @return
     */
    private static Tuple getBeforeDay7(List<Date> list) {
        if(IterUtil.isEmpty(list))
            return null;
        Date beforeDay7=DateTimeCalculatorUtil.reduceAccuracyToDay(DateTimeCalculatorUtil.plusDays(new Date(),-7));//7天之前//减少时间精度到天，其他补0
        Date date1 = DateTimeCalculatorUtil.reduceAccuracyToDay(list.get(0));//减少时间精度到天，其他补0
        Date date2 = DateTimeCalculatorUtil.reduceAccuracyToDay(list.get(1));//减少时间精度到天，其他补0
        int compare1 = DateTimeCalculatorUtil.compare(date1, beforeDay7);
        int compare2 = DateTimeCalculatorUtil.compare(date2, beforeDay7);
        if(compare1 >0 && compare2 >0){
            return new Tuple(Boolean.TRUE,date2,"日期在7天内，包括第7天");
        }
        if(compare1 <=0 && compare2 <=0){
            return new Tuple(Boolean.TRUE,date1,"日期在7天外");
        }
        return new Tuple(Boolean.FALSE,null,"只能查7天内，或者1号到7天前，或者上个月的之前");
    }

    /**
     * 返回2个日期列表，小的在前，大的在后（日期的值并不比较）
     * @param content
     * @return
     */
    private static List<Date> getDates(String content) {
        List<String> listAnd = ReUtil.getAllGroups(REGEX_AND, content,false);
        //System.out.println(listAnd);//[<=, 2020-6-15 00:00:00, >=, 2020-3-13 23:59:59]
        if(!IterUtil.isEmpty(listAnd)){
            Date d1=DateTimeFormatterUtil.smartParseToDate(listAnd.get(1));//第1个日期时间
            Date d2=DateTimeFormatterUtil.smartParseToDate(listAnd.get(3));//第2个日期时间
            if(">=".equals(listAnd.get(0)) ||">".equals(listAnd.get(0))){
                return Arrays.asList(d1,d2);
            }else{
                return Arrays.asList(d2,d1);
            }
        }else{
            List<String> listBetween= ReUtil.getAllGroups(REGEX_BETWEEN, content,false);
            //System.out.println(listBetween);//[2020-3-15 00:00:00, 2020-2-15 23:59:59]
            if(!IterUtil.isEmpty(listBetween)) {
                Date d1 = DateTimeFormatterUtil.smartParseToDate(listBetween.get(0));//第1个日期时间
                Date d2 = DateTimeFormatterUtil.smartParseToDate(listBetween.get(1));//第2个日期时间
                return Arrays.asList(d1, d2);
            }
        }
        return Arrays.asList();
    }
}
